package yxy.game.pm2.active.command.celebration;

import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.acttreasuresignup;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_国庆寻宝;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.currency.Currencies;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.NationalDayMessage;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.module.CurrencyHandle;

import java.util.Collection;
import java.util.Map;

/**
 * @ClassName CMD_Active_国庆寻宝_NationalDayRegisterInfo
 * @Author jingtao-zheng
 * @Date 2022/9/20 15:35
 * @Version 1.0
 **/
@MessageType(proto = Command.NationalDayRegisterInfo, classtype = NationalDayMessage.NationalDayRegisterInfoRequest.class)
public class CMD_Active_国庆寻宝_NationalDayRegisterInfo extends Command<NationalDayMessage.NationalDayRegisterInfoRequest> {
    @Override
    public void execute(MessageSource messageSource, NationalDayMessage.NationalDayRegisterInfoRequest request) throws Exception {
        String playercode = messageSource.getTag();
        NationalDayMessage.NationalDayRegisterInfoResponse.Builder response = NationalDayMessage.NationalDayRegisterInfoResponse.newBuilder();
        try {
            ActiveModule_国庆寻宝 am = ActiveModule_国庆寻宝.get(playercode,false);

            if (am == null) {
                send(messageSource, response, Result.ActNoOpen);
                return;
            }

            if (!am.running()) {
                send(messageSource, response, Result.ActNoOpen);
                return;
            }

            Map<String, acttreasuresignup> allData = DataCenter.getAllData(acttreasuresignup.class);

            for (acttreasuresignup data : allData.values()) {
                NationalDayMessage.NationalDayItem.Builder builder = NationalDayMessage.NationalDayItem.newBuilder();
                builder.setDataId(data.getId());
                builder.setDay(data.getDays());
                Currencies curs = new Currencies(Currency.create(data.getRewardtype(), data.getRewardid(), data.getRewardnum()));
                Collection<PackageMessage.Item> items = new CurrencyHandle().packItems(curs);
                builder.addAllItem(items);//物品
                builder.setDisplay(data.getDisplay());
                response.addItem(builder);
            }

            response.setLogindays(am.getDay());//当前登录的天数
            response.addAllDrawDay(am.getHasRewardDays());//领取的天数
            send(messageSource, response, Result.Success);
        } catch (Exception e) {
            e.printStackTrace();
            send(messageSource, response, Result.DataError);
        }
    }

    private void send(MessageSource messageSource, NationalDayMessage.NationalDayRegisterInfoResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }
}
